﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
using System.Linq;

namespace Yosca
{
	/// <summary>
	/// Interaction logic for TimeCardControl.xaml
	/// </summary>
	public partial class TimeCardControl : UserControl
	{
		public TimeCardControl()
		{
			this.InitializeComponent();
		}

		private void workerClick(object sender, System.Windows.RoutedEventArgs e)
		{
			Database dbase = Database.Instance;
            //creates a worker object and stores it in the database
			Worker proto = new Worker(fName.Text.ToUpper(), lName.Text.ToUpper());

            //lets make sure they have a name
            if (fName.Text == "" || lName.Text == "")
            {
                MessageBox.Show("Please enter a first and last name.");
            }
            else
            {
                dbase.db.Store(proto);
                dbase.db.Commit();
                clearFields();
            }

			IEnumerable<Worker> workerList = from Worker w in dbase.db
											 select w;
			workerGrid.ItemsSource = workerList.ToList();

		}

		private void deleteWorkerClick(object sender, System.Windows.RoutedEventArgs e)
		{
            if ((Worker)(workerGrid.SelectedItem) != null)
            {
                if (MessageBox.Show("Delete worker: " + ((Worker)(workerGrid.SelectedItem)).name.ToUpper() + "?", "Time Card", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
					Database dbase = Database.Instance;
                    dbase.db.Delete((Worker)workerGrid.SelectedItem);
                    dbase.db.Commit();
                    IEnumerable<Worker> workerList = from Worker w in dbase.db
                                                     select w;
                    if (workerList.ToList() != null)
                        workerGrid.ItemsSource = workerList.ToList();
                }
            }
            else
                MessageBox.Show("No worker selected");
		}
		
		private void clearFields()
		{
			fName.Text = ""; lName.Text = "";
			 
		}
	}
}